﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using BusinessLogic;

public partial class Admin_UserManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "ModalPopup", "function showModal() { $find('ShowModal').show(); }", true);
            this.BindGridView();

            //set the name of the CancelControlID to a button inside of the createuserwizard
            Button btnCancel = (Button)CreateUserWizard1.FindControl("btnCancel");

            if(btnCancel != null)
            {
                modalNewUser.CancelControlID = btnCancel.ClientID;
            }
        }
    }

    private void BindGridView()    
    {
        grvUsers.DataSource = AppUser.GetActive(); 
        grvUsers.DataBind();
    }

    protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        Guid id = new Guid(grvUsers.DataKeys[e.RowIndex].Value.ToString());
        
        if(Guid.Empty != id)
        {
            AppUser appUser = new AppUser();
            appUser.ID = id;
            appUser.Delete();
        }

        this.BindGridView();
    }

    protected void RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
            l.Attributes.Add("onclick", "javascript:return confirm('You are about to delete this user account?')");
        }
    }

    //protected void btnSubmit_Click(object sender, EventArgs e)
    //{
    //    SaveNewUser();
    //}

    //private void SaveNewUser()
    //{
    //    //this happens when they click "save" on the modal form
    //    //hide the form first so it seems quicker in the front end
    //    modalNewUser.Hide();

    //    //grab the constituent they entered and create them
    //    Constituent con = ConstituentFromModalForm();
    //    con.Create();

    //    //clear the data they just entered to get ready for next time
    //    ClearControlsOnContainer(pnlNewUser);

    //    //refresh the grid
    //    BindGridView();
    //}

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        //lets get the wizard so that we can get the controls out of it
        CreateUserWizard createUserWizard = (CreateUserWizard)sender;
        Constituent constituent = new Constituent();

        try
        {
            //grab the first name and last name controls
            TextBox txtFirstName = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtFirstName");
            TextBox txtLastName = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtLastName");

            //create the constituent to link to the appuser            
            constituent.FirstName = txtFirstName.Text;
            constituent.LastName = txtLastName.Text;
            constituent.Create();

            //get the id of the newly created user using the username
            AppUser appUser = new AppUser();
            appUser.MembershipUser = Membership.GetUser(createUserWizard.UserName);
            appUser.Constituent = constituent;
            appUser.Create();
        }
        catch (Exception ex)
        {
            Membership.DeleteUser(createUserWizard.UserName);
            constituent.Delete();

            Master.lblMessage.Text = ex.Message;
        }

        BindGridView();
    }

    private void ClearControlsOnContainer(Control container)
    {
        //we hit this code, but the textboxes dont actually get reset.
        //may have to do it from javascript, since the "Save" button never posts back.
        foreach (Control c in container.Controls)
        {
            if (c is TextBox)
            {
                ((TextBox)c).Text = string.Empty;
            }
            else if (c is CheckBox)
            {
                ((CheckBox)c).Checked = false;
            }
            else if (c is DropDownList)
            {
                ((DropDownList)c).ClearSelection();
            }
        }
    }

    protected void btnNewUser_Click(object sender, EventArgs e)
    {
        pnlNewUser.Visible = true;
        modalNewUser.Show();
    }
}